//
//  FindViewController.m
//  FindTest
//
//  Created by lanou3g on 16/3/22.
//  Copyright © 2016年 DH. All rights reserved.
//

#import "FindViewController.h"
#import "RootTableView.h"
#import "FindTableViewCell.h"
#import "WBPopMenuModel.h"

// 二维码 扫一扫依赖的头文件
#import "LBXScanView.h"
#import <objc/message.h>
#import "LBXScanResult.h"
#import "LBXScanWrapper.h"
#import "SubLBXScanViewController.h"
#import "MyQRViewController.h"
#import "AddFrinedViewController.h"

// 朋友圈依赖头文件
#import "SDTimeLineTableViewController.h"
@interface FindTableView : RootTableView

@end

@implementation FindTableView

@end

#pragma mark-----FindViewController-----

@interface FindViewController ()<UITableViewDataSource,UITableViewDelegate>

// 定义tableView
@property (nonatomic,strong) FindTableView *findTabelView;
// 用于判断圈子动态是否有更新
@property (nonatomic,assign) BOOL isNewDynamic;

@end

@implementation FindViewController

- (void)loadView{
    self.findTabelView = [[FindTableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.findTabelView.backgroundColor = [UIColor blackColor];
    self.view = self.findTabelView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
//    设置导航栏和标题栏颜色不相同
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"tabbarHeager"] forBarMetrics:(UIBarMetricsDefault)];
//
//    if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 7.0) {
//        self.edgesForExtendedLayout = UIRectEdgeNone;
//    }
//    UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, Width, 20)];
//    statusBarView.backgroundColor = [UIColor colorWithRed:0.169 green:0.173 blue:0.184 alpha:1.000];
//    [self.navigationController.navigationBar addSubview:statusBarView];
    
    // 设置导航栏和标题栏颜色相同
    // 设置导航栏所有按钮标题和按钮图像的颜色
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    //导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.2 green:0.72 blue:0.46 alpha:1];
    //设置透明度
    self.navigationController.navigationBar.translucent = YES;
    //导航栏标题
    self.navigationItem.title = @"发现";
    //设置字体
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    // 注册cell
    [self.findTabelView.rootTableView registerClass:[FindTableViewCell class] forCellReuseIdentifier:@"findcell"];
    // 设置代理
    self.findTabelView.rootTableView.delegate = self;
    self.findTabelView.rootTableView.dataSource = self;
}

- (NSArray *)getItemArray{
    NSArray *itemArray = @[@"圈子动态",@"iconfont-quanzi",
                           @"扫一扫",@"iconfont-saoyisaoblue",
                           @"小游戏",@"iconfont-xiaoyouxi"
                           ];
    return itemArray;
}

// 用于设置状态栏字体颜色(同时设置View controller-based status bar appearance)
//-(void)viewWillAppear:(BOOL)animated{
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
//}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indentifier = @"Find";
    FindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[FindTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    
    NSArray *ItemArray = [self getItemArray];
    
    cell.nameLabel.text = ItemArray[2*indexPath.section];
    cell.iconImageView.image = [UIImage imageNamed:ItemArray[2*indexPath.section+1]];
    
    // 设置cell右边图片
    UIImage *newImage = [UIImage imageNamed:@"iconfont-mewMessagePerch"];
    if (indexPath.section == 0 && indexPath.row == 0) {
        if (self.isNewDynamic == NO) {
//            圈子动态没有更新赋值圈子中最后一张动态消息的图片，这张图片可以是先缓存在本地
            cell.messageImageView.image = newImage;
        }else{
//            圈子有动态，红点标注
            cell.redView.backgroundColor = [UIColor redColor];
            cell.messageImageView.image = newImage;
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        NSLog(@"圈子动态");
        [self pushFriendCricle];
    }else if (indexPath.section == 1 && indexPath.row == 0){
        NSLog(@"扫一扫");
        if (![self cameraPemission])
        {
            [self showError:@"没有摄像机权限"];
            return;
        }
        
        SEL normalSelector = NSSelectorFromString(@"changeColor");
        if ([self respondsToSelector:normalSelector]) {
            
            ((void (*)(id, SEL))objc_msgSend)(self, normalSelector);
        }
    }else if (indexPath.section == 2 && indexPath.row == 0){
        NSLog(@"小游戏");
    }else{
        NSLog(@"防崩溃");
    }
}

// 跳转到圈子动态
- (void)pushFriendCricle{
    SDTimeLineTableViewController *friendCricle = [[SDTimeLineTableViewController alloc] init];
    [self.navigationController pushViewController:friendCricle animated:YES];
}

#pragma mark -----自定义扫一扫4个角及矩形框颜色-----
- (void)changeColor
{
    
    //设置扫码区域参数
    LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
    style.centerUpOffset = 44;
    
    //扫码框周围4个角的类型设置为在框的上面
    style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_On;
    //扫码框周围4个角绘制线宽度
    style.photoframeLineW = 6;
    
    //扫码框周围4个角的宽度
    style.photoframeAngleW = 24;
    
    //扫码框周围4个角的高度
    style.photoframeAngleH = 24;
    
    //显示矩形框
    style.isNeedShowRetangle = YES;
    
    //动画类型：网格形式，模仿支付宝
    style.anmiationStyle = LBXScanViewAnimationStyle_NetGrid;
    
    
    style.animationImage = [UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_part_net"];
    
    //码框周围4个角的颜色
    style.colorAngle = [UIColor colorWithRed:0.306 green:0.718 blue:0.443 alpha:1.000];
    
    //矩形框颜色
    style.colorRetangleLine = [UIColor colorWithRed:247/255. green:202./255. blue:15./255. alpha:1.0];
    //    [UIColor colorWithRed:0.675 green:0.969 blue:0.923 alpha:1.000]
    //非矩形框区域颜色
    style.red_notRecoginitonArea = 0.675;
    style.green_notRecoginitonArea = 0.969;
    style.blue_notRecoginitonArea = 0.923;
    style.alpa_notRecoginitonArea = 0.2;
    
    SubLBXScanViewController *vc = [SubLBXScanViewController new];
    vc.style = style;
    
    //开启只识别矩形框内图像功能
    vc.isOpenInterestRect = YES;
    vc.isQQSimulator = YES;
    vc.isVideoZoom = YES;
    [self.navigationController pushViewController:vc animated:YES];
    //    [self.selectedViewController presentViewController:vc animated:YES completion:nil];
    
}
// 显示错误信息
- (void)showError:(NSString*)str
{
    [LBXAlertAction showAlertWithTitle:@"提示" msg:str chooseBlock:nil buttonsStatement:@"知道了",nil];
}
// 判断权限
- (BOOL)cameraPemission
{
    
    BOOL isHavePemission = NO;
    if ([AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType:)])
    {
        AVAuthorizationStatus permission =
        [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        
        switch (permission) {
            case AVAuthorizationStatusAuthorized:
                isHavePemission = YES;
                break;
            case AVAuthorizationStatusDenied:
            case AVAuthorizationStatusRestricted:
                break;
            case AVAuthorizationStatusNotDetermined:
                isHavePemission = YES;
                break;
        }
    }
    
    return isHavePemission;
}


- (void)changeNewDynamic{
    self.isNewDynamic = !self.isNewDynamic;
    [self.findTabelView.rootTableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
